<?php
/*
 * Created on 13-sept.-2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class MotionController extends Zend_Controller_Action
{
    public function indexAction()
    {
    	$this->_redirect('/accueil#content');
    }    

    public function launchAction()
    {
        passthru( "/usr/bin/motion");        
        sleep(1); 
        $this->_redirect('/');
    }

    public function quitAction()
    {
        if ( file(C_CONTROL_MOTION . '/0/action/quit') ){
        	sleep(1);        
        	$this->_redirect('/');
        }
    }

    public function detectionAction()
    {
        $get = Zend::registry('fGet');
        $myMotionDetect = $get->getRaw('motionDetect');
        
        $db = Zend::registry('db');
        
        $sql = "SELECT * FROM threads";
        $selectMyThreads = $db->query($sql);
        $myThreads = $selectMyThreads->fetchAll();  

        foreach($myThreads as $thread)
        {
            @file(C_CONTROL_MOTION . '/' . $thread['id'] . '/detection/' . $myMotionDetect);
            continue;
        }
       
        echo "ok";
    }

    public function flushAction()
    {
        $db = Zend::registry('db');

        // Creation de la date butoire avant suppression
        $myDate = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d')-C_MOTION_FLUSHDELAY, date('Y')));

        // Recuperation de la liste des fichiers a flusher
        $sql = "SELECT * FROM security WHERE file_type = '8' AND date < '" . $myDate . "'";
        $mySequencesSelection = $db->fetchAll($sql);
        // Suppression des fichiers sur le disque dur
        foreach ( $mySequencesSelection as $mySequence ){
            $myFile = $mySequence['filename'];
            if ( file_exists( $myFile ) ){
                unlink($myFile);
                echo $myFile;
            }
        }

        // Suppression des entrees en base de donnees
        $sql = "DELETE FROM security WHERE file_type = '8' AND date < '" . $myDate . "'";
        $db->query($sql);        
    }

    public function noRouteAction()
    {
        $this->_redirect('/accueil#content');
    }
}
?>
